package cn.tedu.ivos.dictOption.service.impl;


import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.dict.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dictOption.mapper.DictOptionMapper;
import cn.tedu.ivos.dictOption.pojo.dto.DictOptionParam;
import cn.tedu.ivos.dictOption.pojo.dto.DictOptionQuery;
import cn.tedu.ivos.dictOption.pojo.entity.DictOption;
import cn.tedu.ivos.dictOption.pojo.vo.DictOptionVO;
import cn.tedu.ivos.dictOption.service.DictOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author 482944566@qq.com
 * @version 1.0
 * @date 2024-12-06
 */
@Service
@Slf4j
public class DictOptionServiceImpl implements DictOptionService {
    @Autowired
    private DictOptionMapper dictOptionMapper;
    @Autowired
    private DictMapper dictMapper;
    @Override
    public List<DictOptionVO> selectDictOption(DictOptionQuery dictOptionQuery) {
        log.debug("查询字典项业务: dictOptionQuery={}",dictOptionQuery);
        List<DictOptionVO> list = dictOptionMapper.select(dictOptionQuery);
        return list;
    }

    @Override
    public void saveDictOption(DictOptionParam dictOptionParam) {
        log.debug("保存字典项业务：dictOptionParam={}",dictOptionParam);
        DictOption dictOption = new DictOption();
        BeanUtils.copyProperties(dictOptionParam,dictOption);
        if (dictOption.getId()==null){
            dictOption.setCreateTime(new Date());
            dictOptionMapper.insert(dictOption);
        }else{
            dictOption.setUpdateTime(new Date());
            dictOptionMapper.update(dictOption);
        }
    }

    @Override
    public void deleteDictOption(Long id) {
        log.debug("删除字典项业务：id={}",id);
        dictOptionMapper.delete(id);
    }

    @Override
    public List<DictOptionVO> selectDictOptionByCode(String code) {
        log.debug("根据字典code查询对应的所有字典项业务：code={}",code);
        DictQuery dictQuery = new DictQuery();
        dictQuery.setCode(code);
        List<DictVO> dictVOList = dictMapper.selectDictByCode(dictQuery);
        if (dictVOList!=null && dictVOList.size()>0){
            DictVO dictVO = dictVOList.get(0);
            DictOptionQuery dictOptionQuery = new DictOptionQuery();
            dictOptionQuery.setDictId(dictVO.getId());
            List<DictOptionVO> dictOptionVOList = dictOptionMapper.select(dictOptionQuery);
            return dictOptionVOList;
        }else{
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }

    }
}
